package EA.testproblems;
import EA.*;
import RKUjava.util.*;

/**
   This testproblem is from De Jong's original test suite containing five
   functions. <br><br>

   <table border="0" cellpadding="2" cellspacing="0">
   <tr bgcolor="#a0a0a0">
   <td colspan="2" valign="top"><b>Problem description</b></td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top" width="200"><b>Name:</b></td>
   <td valign="top">De Jong F2</td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>Nickname:</b></td>
   <td valign="top">&nbsp;</td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>Intended usage:</b></td>
   <td valign="top">&nbsp;</td>
   </tr>

   <tr>
   <td colspan="2" valign="top">&nbsp;</td>
   </tr>
   <tr bgcolor="#a0a0a0">
   <td colspan="2" valign="top"><b>Problem details</b></td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>Function:</b></td>
   <td valign="top">100(x<sup>2</sup>-y)<sup>2</sup> + (1-x)<sup>2</sup></td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>Plots:</b></td>
   <td valign="top"><img src="../../images/testproblems/dejongf2.gif">&nbsp;&nbsp;
   <img src="../../images/testproblems/dejongf2_contour.gif"></td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>Ranges:</b></td>
   <td valign="top">x = [-2.048:2.048]&nbsp;&nbsp;y = [-2.048:2.048] </td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>Type:</b></td>
   <td valign="top">Minimization</td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>No. of maximas:</b></td>
   <td valign="top">?</td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>No. of minimas:</b></td>
   <td valign="top">1+</td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>Optima radius:</b></td>
   <td valign="top">0.2</td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>Optima descriptions:</b></td>
   <td valign="top">&nbsp;</td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>Known optimas:</b></td>
   <td valign="top">
   GMIN(1.0,1.0)
   <br><font size=1>Capital letters 
   means that the precise optima is known, lowercase letters is the best known 
   so far.</font></td>
   </tr>
   <tr>
   <td colspan="2" valign="top">&nbsp;</td>
   </tr>
   <tr bgcolor="#a0a0a0">
   <td colspan="2" valign="top"><b>Plotting details</b></td>
   </tr>
   
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>GNUPlot code:</b></td>
   <td valign="top">
   set hidden3d<br>
   set isosamples 50<br>
   set view 70,15<br>
   splot [-2.048:2.048] [-2.048:2.048] 100*((x*x-y)**2) + (1-x)**2
   </td>
   </tr>
   <tr bgcolor="#e0e0e0">
   <td valign="top"><b>Latex code:</b></td>
   <td valign="top">
   De Jong F2:<br>
   \[<br>
   f(x,y) = 100(x^2-y)^2 + (1-x)^2\\[-2mm]<br>
   \]<br>
   where\\<br>
   \vspace*{-2mm}<br>
   \[<br>
   -2.048\leq{}x\leq{}2.048\; \textrm{ and } -2.048\leq{}y\leq{}2.048<br>
   \]<br>
   </td>
   </tr>


   </table>
*/

public class DeJongF2 extends NumericalProblem 
{

  // Easier way to build max and min
    private double[][] lmax = new double[0][2];
    private double[][] lmin = {{1,1}};

  public DeJongF2()
    {
      super();

      double[] optimas;

      name = "De Jong function F2";
      objectivefunction = new NumericalFitness(){
	public double Fitness_calcFitness_inner(double[] realpos)
	{
	  //	  System.out.println("ad"+RKUStringUtils.arrayToString(realpos)+" fit="+(100*(Math.pow(((realpos[0]*realpos[0]) - realpos[1]),2)) + Math.pow((1-realpos[0]),2)));

	  return 100*(Math.pow(((realpos[0]*realpos[0]) - realpos[1]),2)) + 
	  Math.pow((1-realpos[0]),2);
	  
	};
      };

      dimensions = 2;
      ismaximization = false;
      optimumradius = 0.2;

      intervals = new Interval[2];
      intervals[0] = new Interval(-2.048, 2.048);
      intervals[1] = new Interval(-2.048, 2.048);
      

      // Set up known maximas
      knownmaxima = new NumericalOptimum[lmax.length];

      for (int i=0;i<lmax.length;i++) {
	optimas = new double[dimensions];
	optimas[0] = lmax[i][0];
	optimas[1] = lmax[i][1];
	knownmaxima[i] = new NumericalOptimum(optimas, objectivefunction.calcFitness(optimas), true, false, i);
      }

      // Set up known minimas
      knownminima = new NumericalOptimum[lmin.length];

      for (int i=0;i<lmin.length;i++) {
	optimas = new double[dimensions];
	optimas[0] = lmin[i][0];
	optimas[1] = lmin[i][1];
	knownminima[i] = new NumericalOptimum(optimas, objectivefunction.calcFitness(optimas), false, false, i);
      }
    }
}
